package com.muyi.app;

import com.muyi.spring.annotation.Component;
import com.muyi.spring.annotation.Value;
import com.muyi.spring.beans.BeanPostProcessor;

import java.lang.reflect.Field;

@Component
public class ValueBeanPostProcesser implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) {
        Field[] declaredFields = bean.getClass().getDeclaredFields();
        for (Field f : declaredFields) {
            if (f.isAnnotationPresent(Value.class)) {
                Value v = f.getAnnotation(Value.class);
                f.setAccessible(true);
                try {
                    f.set(bean, v.value());
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        return bean;
    }
}
